<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="考试日期" prop="examDate">
        <el-date-picker
          v-model="queryParams.examDate"
          type="date"
          placeholder="请选择考试日期"
          value-format="yyyy-MM-dd"
          style="width: 240px"
        />
      </el-form-item>
        <!--<el-form-item label="归属考试" prop="examId">-->
            <!--<el-select v-model="queryParams.examId" placeholder="请选择归属考试" style="width: 100%">-->
                <!--<el-option v-for="item in examOptions" :key="item.id" :label="item.examName" :value="item.id" />-->
            <!--</el-select>-->
        <!--</el-form-item>-->
      <!--<el-form-item label="考试类型" prop="examTypeId">-->
        <!--<el-select v-model="queryParams.examTypeId" placeholder="请选择考试类型" clearable style="width: 240px">-->
          <!--<el-option v-for="item in examTypeOptions" :key="item.id" :label="item.name" :value="item.id" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item label="考点名称" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入考点名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:schedules:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:schedules:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:schedules:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 数据表格 -->
    <el-table v-loading="loading" :data="schedulesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="考试日期" align="center" prop="examDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.examDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="考试类型" align="center" prop="examTypeName" :show-overflow-tooltip="true" />
      <el-table-column label="对应等级/领域" align="center" prop="examLevelName" :show-overflow-tooltip="true" />
      <el-table-column label="开考时间" align="center" prop="examTime" width="120" />
      <el-table-column label="考点名称" align="center" prop="location" :show-overflow-tooltip="true" />
      <el-table-column label="报名截止" align="center" prop="regDeadline" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regDeadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归属考试" align="center" prop="examName" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:schedules:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:schedules:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:schedules:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 新增/编辑弹窗 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="考试日期" prop="examDate">
              <el-date-picker
                v-model="form.examDate"
                type="date"
                placeholder="请选择考试日期"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">-->
            <!--<el-form-item label="考试类型" prop="examTypeId">-->
              <!--<el-select v-model="form.examTypeId" placeholder="请选择考试类型" style="width: 100%">-->
                <!--<el-option v-for="item in examTypeOptions" :key="item.id" :label="item.name" :value="item.id" />-->
              <!--</el-select>-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->
        <!--<el-row>-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="对应等级/领域" prop="examLevelId">-->
              <!--<el-select v-model="form.examLevelId" placeholder="请选择等级/领域" style="width: 100%">-->
                <!--<el-option v-for="item in examLevelOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue" />-->
              <!--</el-select>-->
            <!--</el-form-item>-->
          <!--</el-col>-->
          <el-col :span="12">
            <el-form-item label="开考时间" prop="examTime">
              <el-time-picker
                v-model="form.examTime"
                placeholder="请选择开考时间"
                value-format="HH:mm"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考点名称" prop="location">
              <el-input v-model="form.location" placeholder="请输入考点名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报名截止" prop="regDeadline">
              <el-date-picker
                v-model="form.regDeadline"
                type="date"
                placeholder="请选择报名截止日期"
                value-format="yyyy-MM-dd"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="归属考试" prop="examId">
              <el-select v-model="form.examId" placeholder="请选择归属考试" style="width: 100%">
                <el-option v-for="item in examOptions" :key="item.id" :label="item.examName" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" :rows="3" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情弹窗 -->
    <el-dialog title="考试安排详情" :visible.sync="detailOpen" width="800px" append-to-body>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="考试日期">
          {{ parseTime(schedulesInfo.examDate, '{y}-{m}-{d}') || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="考试类型">
          {{ schedulesInfo.examTypeName || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="对应等级/领域">
          {{ schedulesInfo.examLevelName || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="开考时间">
          {{ schedulesInfo.examTime || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="考点名称">
          {{ schedulesInfo.location || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="报名截止">
          {{ parseTime(schedulesInfo.regDeadline, '{y}-{m}-{d}') || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="归属考试" :span="2">
          {{ schedulesInfo.examName || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">
          {{ schedulesInfo.remark || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建人">
          {{ schedulesInfo.createBy || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="创建时间">
          {{ parseTime(schedulesInfo.createTime) || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="更新人">
          {{ schedulesInfo.updateBy || '暂无' }}
        </el-descriptions-item>
        <el-descriptions-item label="更新时间">
          {{ parseTime(schedulesInfo.updateTime) || '暂无' }}
        </el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleEditFromDetail">编辑</el-button>
        <el-button @click="detailOpen = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchedules, getSchedules, delSchedules, addSchedules, updateSchedules } from "@/api/system/schedules"
import { listExamstype } from "@/api/system/examstype"
import { listExams } from "@/api/system/exams"
import { getDicts } from "@/api/system/dict/data"

export default {
  name: "Schedules",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 考试安排表格数据
      schedulesList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      detailOpen: false,
      // 考试安排详情信息
      schedulesInfo: {},
      // 日期范围
      dateRange: [],
      // 考试类型选项
      examTypeOptions: [],
      // 考试选项
      examOptions: [],
      // 等级/领域选项
      examLevelOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        examId: undefined,
        examDate: undefined,
        examTypeId: undefined,
        examLevelId: undefined,
        location: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        examDate: [
          { required: true, message: "考试日期不能为空", trigger: "blur" }
        ],
        examTypeId: [
          { required: true, message: "考试类型不能为空", trigger: "change" }
        ],
        examLevelId: [
          { required: true, message: "对应等级/领域不能为空", trigger: "change" }
        ],
        examTime: [
          { required: true, message: "开考时间不能为空", trigger: "blur" }
        ],
        location: [
          { required: true, message: "考点名称不能为空", trigger: "blur" }
        ],
        regDeadline: [
          { required: true, message: "报名截止日期不能为空", trigger: "blur" }
        ],
        examId: [
          { required: true, message: "归属考试不能为空", trigger: "change" }
        ]
      }
    }
  },
  created() {
    this.getList()
    this.getExamTypeOptions()
    this.getExamOptions()
    this.getExamLevelOptions()
  },
  methods: {
    /** 查询考试安排列表 */
    getList() {
      this.loading = true
      listSchedules(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.schedulesList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    /** 获取考试类型选项 */
    getExamTypeOptions() {
      listExamstype({pageNum:1,pageSize:1000}).then(response => {
        this.examTypeOptions = response.rows
      })
    },
    /** 获取考试选项 */
    getExamOptions() {
      listExams({pageNum:1,pageSize:1000}).then(response => {
        this.examOptions = response.rows
      })
    },
    /** 获取等级/领域选项 */
    getExamLevelOptions() {
      getDicts("exam_level").then(response => {
        this.examLevelOptions = response.data
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        examDate: undefined,
        examTypeId: undefined,
        examLevelId: undefined,
        examTime: undefined,
        location: undefined,
        regDeadline: undefined,
        examId: undefined,
        remark: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加考试安排"
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      this.reset()
      getSchedules(row.id).then(response => {
        this.schedulesInfo = response.data
        this.detailOpen = true
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getSchedules(id).then(response => {
        this.form = response.data
        this.form.examId = this.form.examId ? Number(this.form.examId) : ''
        this.open = true
        this.title = "修改考试安排"
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSchedules(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSchedules(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除考试安排编号为"' + ids + '"的数据项？').then(function() {
        return delSchedules(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 从详情弹窗跳转到编辑弹窗 */
    handleEditFromDetail() {
      this.detailOpen = false
      this.form = { ...this.schedulesInfo }
      this.open = true
      this.title = "修改考试安排"
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/schedules/export', {
        ...this.queryParams
      }, `schedules_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
